package cn.ges.date;

import cn.ges.string.StringUtil;

import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author gespent@163.com
 * @date 2018/12/5
 */
public class DateUtil {
    public final static long ONE_HOUR_OF_SECONDS = 3600L;
    private final static DateTimeFormatter DF = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private final static DateTimeFormatter DF2 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    private final static DateTimeFormatter DF1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private final static DateTimeFormatter DF3 = DateTimeFormatter.ofPattern("HH:mm");

    public static String formatSecond2Hms(int second) {
        int hour = second / 3600;
        second = second % 3600;
        int minute = second / 60;
        second = second % 60;
        return StringUtil.format("{}时{}分{}秒", hour, minute, second);
    }

    public static long getSecond(@NotNull LocalDateTime localDateTime) {
        return localDateTime.toEpochSecond(ZoneOffset.of("+8"));
    }

    /**
     * UTC标准格式转date -> 2017-09-18T16:00:00.000Z
     *
     * @param date 转换完成的date
     * @return date 或者Null
     */
    public static Date UTC2Date(String date) {
        //String date = "2017-09-18T16:00:00.000Z";
        //是空格+UTC
        date = date.replace("Z", " UTC");
        //格式化的表达式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        try {
            return format.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 返回现在到明日0点的秒数
     */
    public static Long nowToWeeHours() {
        final int second = LocalDate.now().atTime(23, 59, 59, 999).getSecond();
        return second - System.currentTimeMillis() / 1000;
    }

    /**
     * 返回现在到某个时间的秒数
     */
    public static Long nowUntilSecond(LocalDateTime targetTime) {
        final long l1 = targetTime.toEpochSecond(OffsetDateTime.now().getOffset());
        long l = l1 - System.currentTimeMillis() / 1000;
        if (l < 0) {
            l = 0L;
        }
        return l;
    }


    /**
     * yyyy-MM-dd HH:mm:SS 格式字符串转换为LocalDateTime
     */
    public static LocalDateTime stringToTimeStamp(String time) {
        return LocalDateTime.parse(time, DF1);
    }


    /***
     * yyyy-MM-dd'T'HH:mm:ss.SSS Z 格式字符串转换为LocalDateTime
     */
    public static LocalDateTime stringUTC2DateTime(String time) {
        return LocalDateTime.parse(time, DF2);
    }

    /**
     * instant转LocalDateTime
     */
    public static LocalDateTime instantToLocalDateTime(Instant instant) {
        return instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    /**
     * String yyyy-MM-dd 转LocalDate
     */
    public static LocalDate stringToDate(String time) {
        return LocalDate.parse(time, DF);
    }

    /**
     * 将当前时间以yyyy-MM-dd的形式返回
     */
    public static String dateToString() {
        return DF.format(LocalDate.now());
    }

    /**
     * 将一个localDate 转化为yyyy-MM-dd格式字符串
     */
    public static String dateToString(LocalDate time) {
        return DF.format(time);
    }

    /**
     * 将当前时间以yyyy-MM-dd HH:mm:SS 的形式返回
     */
    public static String dateToStringWithHms() {
        return DF1.format(LocalDateTime.now());
    }

    /**
     * 将时间以 hh:mm 格式返回
     */
    public static String dateToHm(LocalDateTime time) {
        return DF3.format(time);
    }


    /**
     * 将一个localDate 转化为yyyy-MM-dd HH:mm:ss格式字符串
     */
    public static String dateToStringWithHms(LocalDateTime time) {
        return DF1.format(time);
    }

    /**
     * 将一个localDateTime转为Instant
     */
    public static Instant localDateTime2Instant(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        return localDateTime.toInstant(OffsetDateTime.now().getOffset());
    }
    


}
